package mww.javabean.service;

import java.util.Map;

import mww.common.DaoException;
import mww.common.ServiceException;
import mww.javabean.dao.TeacherCDao;
import mww.javabean.dao.TeacherCDaoImpl;
import mww.javabean.entity.TeacherC;

public class TeacherCServiceImpl implements TeacherCService {

	private TeacherCDao tcDao = new TeacherCDaoImpl();
	@Override
	public void addTeacherC(TeacherC teacherC)  throws ServiceException{
		try{
			tcDao.addTeacherC(teacherC);
		}catch(DaoException e){
			e.printStackTrace();
			throw new DaoException(e.getMessage(),e);
		}
	}

	@Override
	public void delTeacherC(String teacherNo, String courseNo,Integer tcTeacherNo)  throws ServiceException{
		try{
			tcDao.delTeacherC(teacherNo, courseNo,tcTeacherNo);
		}catch(DaoException e){
			e.printStackTrace();
			throw new DaoException(e.getMessage(),e);
		}
	}

	@Override
	public Map findTeacherCByTeacherNo(String teacherNo, Integer status, Integer curPage) throws ServiceException {
		Map map = null;
		try{
			map = tcDao.findTeacherCByTeacherNo(teacherNo, status, curPage);
		}catch(DaoException e){
			e.printStackTrace();
			throw new DaoException(e.getMessage(),e);
		}
		return map;
	}
	@Override
	public Map findAllTeacherC(Integer curPage) throws ServiceException {
		Map map = null;
		try{
			map = tcDao.findAllTeacherC(curPage);
		}catch(DaoException e){
			e.printStackTrace();
			throw new DaoException(e.getMessage(),e);
		}
		return map;
	}

	@Override
	public Map findAllTeacherCByMostCon(TeacherC condition, Integer curPage) throws ServiceException {
		Map map = null;
		try{
			map = tcDao.findAllTeacherCByMostCon(condition, curPage);
		}catch(DaoException e){
			e.printStackTrace();
			throw new DaoException(e.getMessage(),e);
		}
		return map;
	}

	@Override
	public String findTeacherNo(Integer tcTeacherNo, String courseNo) throws ServiceException {
		String teacherNo = null;
		try{
			teacherNo = tcDao.findTeacherNo(courseNo, tcTeacherNo);
		}catch(DaoException e){
			e.printStackTrace();
			throw new DaoException(e.getMessage(),e);
		}
		return teacherNo;
	}

	@Override
	public void incTcSel(String teacherNo, String courseNo,Integer tcTeacherNo) throws ServiceException {
		try{
			tcDao.incTcSel(teacherNo, courseNo,tcTeacherNo);
		}catch(DaoException e){
			e.printStackTrace();
			throw new DaoException(e.getMessage(),e);
		}
	}

	@Override
	public void decTcSel(String teacherNo, String courseNo,Integer tcTeacherNo) throws ServiceException {
		try{
			tcDao.decTcSel(teacherNo, courseNo,tcTeacherNo);
		}catch(DaoException e){
			e.printStackTrace();
			throw new DaoException(e.getMessage(),e);
		}
	}

	@Override
	public void updateScoreInTeacherC(String teacherNo, String courseNo ,Integer tcTeacherNo, float maxScore, float minScore, float avgScore)
			throws ServiceException {
		try{
			tcDao.updateScoreInTeacherC(teacherNo, courseNo,tcTeacherNo, maxScore, minScore, avgScore);
		}catch(DaoException e){
			e.printStackTrace();
			throw new DaoException(e.getMessage(),e);
		}
	}

	@Override
	public boolean checkTeacherCExistByCourseNo(String courseNo) throws ServiceException {
		boolean exist = true;
		try{
			exist = tcDao.checkTeacherCExistByCourseNo(courseNo);
		}catch(DaoException e){
			e.printStackTrace();
			throw new DaoException(e.getMessage(),e);
		}
		return exist;
	}

	@Override
	public boolean checkTeacherCExistByTeacherNo(String teacherNo) throws ServiceException {
		boolean exist = true;
		try{
			exist = tcDao.checkTeacherCExistByTeacherNo(teacherNo);
		}catch(DaoException e){
			e.printStackTrace();
			throw new DaoException(e.getMessage(),e);
		}
		return exist;
	}

	@Override
	public boolean checkExistConflict(String teacherNo, String courseNo,Integer tcTeacherNo) throws ServiceException {
		boolean exist = true;
		try{
			exist = tcDao.checkExistConflict(teacherNo, courseNo,tcTeacherNo);
		}catch(DaoException e){
			e.printStackTrace();
			throw new DaoException(e.getMessage(),e);
		}
		return exist;
	}

	@Override
	public boolean checkTimeConflict(String teacherNo, String tcTime) throws ServiceException {
		boolean exist = true;
		try{
			exist = tcDao.checkTimeConflict(teacherNo, tcTime);
		}catch(DaoException e){
			e.printStackTrace();
			throw new DaoException(e.getMessage(),e);
		}
		return exist;
	}

	@Override
	public TeacherC findTeacherCById(String teacherNo, String courseNo, Integer tcTeacherNo) throws ServiceException {
		TeacherC teacherc = new TeacherC();
		try{
			teacherc = tcDao.findTeacherCById(teacherNo, courseNo, tcTeacherNo);
		}catch(DaoException e){
			e.printStackTrace();
			throw new DaoException(e.getMessage(),e);
		}
		return teacherc;
	}

	@Override
	public boolean isFull(String teacherNo, String courseNo, Integer tcTeacherNo) throws ServiceException {
		boolean fullFlag = true;
		try{
			TeacherC teacherc = new TeacherC();
			teacherc = tcDao.findTeacherCById(teacherNo, courseNo, tcTeacherNo);
			if(teacherc.getTcSel()>=teacherc.getTcCapacity())
				fullFlag = true;
			else
				fullFlag = false;
		}catch(DaoException e){
			e.printStackTrace();
			throw new DaoException(e.getMessage(),e);
		}
		return fullFlag;
	}

}
